package com.ideabove.heart.game;




import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.View;

import com.ideabove.heart.MoeActivity;
import com.ideabove.heart.data.BitmapObj;
import com.ideabove.heart.data.Comment;
import com.ideabove.heart.data.Episode;
import com.ideabove.heart.game.character.EpPlayGirl;
import com.ideabove.heart.game.framework.GameView;
import com.ideabove.heart.manager.MoeManager;
import com.ideabove.heart.manager.ScheduleManager;
import com.ideabove.heart.util.MyUtil;
import com.ideabove.heart.util.MyUtil.ImageTouch;

public class EpPlayState extends StoryGirlState{

	BitmapObj btnRepeat;
	BitmapObj btnList;

	public EpPlayState(GameView gameView, Episode ep){
		super(gameView, ep);
		
		// 다시듣기 버튼
		MoeManager.addImg("btn_story_repeat.png");		
		Rect repeatTouch = new Rect(
				20,
				(int)(25),
				20 + (int)(MoeManager.getImg("btn_story_repeat.png").getWidth()),
				25 + (int)(MoeManager.getImg("btn_story_repeat.png").getHeight()));
		
		btnRepeat = new BitmapObj(
				MoeManager.getImg("btn_story_repeat.png"),
				repeatTouch.left,
				repeatTouch.top,
				repeatTouch
				);
		
		
		// 통화 리스트로
		MoeManager.addImg("btn_stroy_list.png");
		int btnListX = (int)(MoeManager.screenWidth - MoeManager.getImg("btn_stroy_list.png").getWidth() 
				- (20 * (MoeManager.screenWidth / MoeManager.DEFAULT_SC_WIDTH)));
		int btnListY = 25;		
		Rect nextTouch = new Rect(
				btnListX,
				btnListY,
				btnListX + (int)(MoeManager.getImg("btn_stroy_list.png").getWidth()),
				btnListY + (int)(MoeManager.getImg("btn_stroy_list.png").getHeight()));
		
		btnList = new BitmapObj(
				MoeManager.getImg("btn_stroy_list.png"),
				nextTouch.left,
				nextTouch.top,
				nextTouch
				);
		
	}
	
	
	
	@Override
	protected void initGirlEpisode(Episode ep) {
		// TODO Auto-generated method stub
		AssetFileDescriptor afd;
		try{
			if(ep.getBgSound() != null){			
				afd = MoeManager.mCurContext.getAssets().openFd("voice/" + ep.getBgSound());
				backgroundMp = new MediaPlayer();
				backgroundMp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());	
				backgroundMp.setLooping(true);
				backgroundMp.prepare();
				backgroundMp.start();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		ArrayList<Comment> comments = ep.getComment();
		for(Comment cmt : comments){
			EpPlayGirl character = new EpPlayGirl(cmt);
			character.loadResource();
			situationArray.add(character);			
		}
		
		if(situationArray.size() > 0){
			lastCharatcer = situationArray.get(situationArray.size() - 1);
		}
		character = situationArray.get(curStateIdx);
		changeCharacter(character);
	}



	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub

		MyUtil.touchMe(null, event, new ImageTouch() {

			@Override
			public void touch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				// 확인,다음
				if(btnConfirm.isBoundary(event)){					
					if(!moveNextState()){   
						if("ep30".equals(ep.getId())){
							ScheduleManager sm = new ScheduleManager((Activity)MoeManager.mCurContext);
							sm.episodeDone();
							
							MoeManager.clearImages();
							
							MoeManager.setWhatWillIdo(MoeManager.DO_ENDING); // 메인에서 모에를 호출함.
							Intent intent = new Intent(MoeManager.mCurContext, MoeActivity.class);
							
							((Activity)MoeManager.mCurContext).finish();
							MoeManager.mCurContext.startActivity(intent);							
						}else{
							((Activity)MoeManager.mCurContext).finish();
							MoeManager.setWhatWillIdo(MoeManager.DO_NOTHING);
						}						
					}
				}
				
				// 반복
				if(btnRepeat.isBoundary(event)){
					((EpPlayGirl)character).repeatVoice();
				}
				
				// 리스트				
				if(btnList.isBoundary(event)){					   
					((Activity)MoeManager.mCurContext).finish();
					MoeManager.setWhatWillIdo(MoeManager.DO_NOTHING);
				}
			}
		});


		return false;
	}

	@Override
	public void Render(Canvas canvas) {
		// TODO Auto-generated method stub
		character.draw(canvas);

		btnConfirm.draw(canvas);
		btnRepeat.draw(canvas);
		btnList.draw(canvas);
	}
}
